toast('Hello, AutoX.js');


/**
 * @description launcher
 */

try {
    auto.waitFor();
} catch (e) {
    auto();
}


// 导入其他脚本文件
// var network = require('network.js');
// var ui = require('ui.js');
var f = require('f.js');
f.init()
var item = require('itemlist.js');
var post_ = require('post.js');
var addressList = require('address.js');
var change = require('change.js');

var applist = ["com.sankuai.meituan","com.xunmeng.pinduoduo","ctrip.android.view"]
var spot_list = [
    "13812345678",
    "13987654321",
    "13700000000",
    "13612345678",
    "13523456789",
    "13434567890",
    "15012345678",
    "15800000000",
    "15987654321"
  ];

var appinfo = {}
appinfo.address = f.getLocal('address') || 0
appinfo.item = f.getLocal('item') || 0
appinfo.app = f.getLocal('app') || 0
appinfo.date = f.getLocal('date') || f.getCurrentDate()



function update(){
    if ( appinfo.date != f.getCurrentDate() ){
        appinfo.date = f.getCurrentDate()
        f.setLocal('date', appinfo.date)
        appinfo.address = 0
        appinfo.item = 0
        f.setLocal('address', appinfo.address)
        f.setLocal('item', appinfo.item)
    }
}



function mtapp(){

    var bid = applist[0]
    f.active(bid, 5)


    // 打开拼多多
    f.ms({"desc":"超市便利"}, true, 2)
   
}



appinfo.item = 0
appinfo.address = 1
appinfo.app = 0
// appinfo.search = 'find_all'
appinfo.shop = f.getLocal('shop') || ''
var address_List = addressList.address()


// change.lcal()
// log("appinfo.item", appinfo.item, "item", item.itemlist().length-1)

// while (appinfo.item < item.itemlist().length){
//     update()
//     if ( appinfo.search == 'find_all'){
//         mtapp()
//     }else{
//         mtapp_one()
//     }
//     back()
//     sleep(1000)
// }
// // 主脚本的其他逻辑代码
// // ...
// back()
// sleep(1000)
// back()
// sleep(1000)
// f.notice(appinfo.address + '采集完成 > ' + address_List[appinfo.address])


//批查打印app参数
function printAll( ways ) {
    log('ways', ways)
    if (!ways){
        ways = 'className'
    }
    log('ways', ways)
    if (ways = 'className'){
        var all_Info = classNameMatches(/.*/).find();
    }else
    if (ways == 'text'){
        log('----------text--------')
        var all_Info = textMatches(/^[\u4e00-\u9fa5]{5,}$/).find();
    }else
    if (ways == 'id'){
        log('----------id--------')
        var all_Info = idMatches(/.*/).find();
    }else{
        log('------2222----------')
        eval( 'var all_Info = ' + ways + '.find();'  )
    }
    
    if ( all_Info ){
        for (var i = 0;i<all_Info.length;i++){
            var d = all_Info[i];
            // log(i,d.id(),d.text(),"desc:"+d.desc(),'"className":"'+d.className()+'"',
            // "clickable->"+d.clickable(),'selected->'+selected(),"depth->"+d.depth(),
            // d.bounds().centerX(),d.bounds().centerY())

            log(d.text() )
        }
    }else{
        log('没有找到任何相关的节点')
    }
}





function extractPrice(str) {
    // 正则表达式匹配两个¥符号或一个¥符号
    const regex = /¥(\d+)|¥{2}(\d+)/;
    const match = str.match(regex);

    // 如果匹配成功，根据匹配结果提取数字
    if (match) {
        if (match[1]) { // 如果只有一个¥
            return match[1];
        } else if (match[2]) { // 如果有两个¥
            return match[2];
        }
    }
    // 如果没有匹配到，返回undefined或错误信息
    return null;
}

function printAll( ways ) {
    if (!ways){
        ways = 'className'
    }
    if (ways == 'className'){
        var all_Info = classNameMatches(/.*/).find();
    }else
    if (ways == 'text'){
        var all_Info = textMatches(/.*/).find();
    }else
    if (ways == 'id'){
        var all_Info = idMatches(/.*/).find();
    }else{
        eval('var all_Info = ' + ways + '.find();')
    }

    var price_ = []
    var name_ = []

    if ( all_Info ){
        for (var i = 0;i<all_Info.length;i++){
            var d = all_Info[i];
            if ( d.desc() && d.desc().includes('\ufffc') ){
                // log(i,d.id(),d.text(),"desc:"+d.desc(),'"className":"'+d.className()+'"',
                // "clickable->"+d.clickable(),'selected->'+selected(),"depth->"+d.depth(),
                // d.bounds().centerX(),d.bounds().centerY())
                // log("===========", d.desc())

                var texts = d.desc().split('\n')
                texts.forEach(element => {
                    if ( element.includes('¥') ){
                        log("第二个¥符号后的数字:",extractPrice(element));
                        price_.push(extractPrice(element))
                    }
                });

                if ( texts[0].length > 5 ){
                    log(">>>>>", texts[0])
                    name_.push(texts[0].replace(/\uFFFC/g, ''))
                }
            }
        }

        log("price_", price_)
        log("name_", name_)

        for (var i = 0;i<name_.length;i++){
            var arr = {
                "ids":hotal.id,
                "idname":hotal.name,
                "name":name_[i],
                "price":price_[i],
            }
    
            var post_data = {
                s:'App.Index.Update_where',                    
                table:'jlcj_jdmc',
                arr:JSON.stringify(arr),
                where:JSON.stringify({
                    "ids": hotal.id,
                    "name": name_[i],
                    })
            }

            log(post_data)
            post_.post_wenfree(post_data)

            var arr = {
                "product": name_[i],
                "city": hotal.name,
                "price":price_[i],
                "pdate":appinfo.date,
                'mall':'携程',
                'shop': hotal.name,
                'code':'',
                'spot': 18128823268,
                'title': hotal.name,
                'unit': '间',
            }
            log(post_.post_ybl(arr))
        }

    }else{
        log('没有找到任何相关的节点')
    }
}


function online(){
    var url = 'http://mgcj.dthzsoft.com'
    var data = {
        table: 'device_list',
        s:'App.Index.update_where',
        arr:JSON.stringify({ "imei": device.getAndroidId(), state:'在线' }),
        where:JSON.stringify({imei: device.getAndroidId() })
    }
    log(data)
    var r = http.post(url, data);
    return r.body.json();
}


function getTask(){
    var url = 'http://mgcj.dthzsoft.com/?s=App.Task.Select&imei='+device.getAndroidId()
    var r = http.get(url);
    return r.body.json();
}

function getBack(id){
    var url = 'http://mgcj.dthzsoft.com/?s=App.Task.Back&id='+id
    var r = http.get(url);
    return r.body.json();
}



function get_hotal(id){
    var url = 'http://mgcj.dthzsoft.com'
    var data = {
        table: 'jlcj_xcjd',
        s:'App.Index.Select_where',
        page:1,
        size:1,
        where:JSON.stringify({id:id})
    }
    var r = http.post(url, data);
    return r.body.json();
}






function pachong(){
    // hotal = f.getLocal('hotal') || []
    // if ( hotal.length == 0 ){
    //     hotal = get_hotal().data.data[0]
    //     f.setLocal('hotal', hotal)
    // }

    // hotal = get_hotal().data.data[0]
    // f.setLocal('hotal', hotal)

    back()
    sleep(2000)
    back()
    sleep(2000)

    var bid = "ctrip.android.view"
    f.active(bid, 5);

    f.ms({"text":"酒店"}, true, 2)
    f.ms({"textMatches":"位置\.*", "id":"a" }, true, 2)


    setText(0, hotal.name)
    log("hotal", hotal.name)
    sleep(2000)
    f.ms({"textMatches": "\.*" + hotal.name + '\.*', "depth": 12}, true, 2)
    f.ms({"text": "查 询" }, true, 2)

    for (var i = 0;i<5;i++){
        printAll("className")
        f.moveTo(500, 1000, 500, 200, 2000)
        f.moveTo(500, 1000, 500, 200, 2000)
    }

    getBack(id)

    back()
    sleep(2000)
    back()
    sleep(2000)
    
}
// console.show();
var id = "";
while ( true ){
    online()
    r = getTask()
    log(r)
    if ( r.data == "暂无任务" ){
        toast("暂无任务")
        sleep(3000)
    }else{
        
        r2 = get_hotal(r.data.info.address)
        hotal = r2.data.data[0]
        log(hotal)
        id = r.data.id
        toast(hotal.name)
        pachong()
    }
    sleep(10000);
}







toast("end")


